<?php

require_once('modelField.php');
require_once('modelFields.php');

class model
{
	public $className;
	public $table;
	public $tableLocale;
	public $tableView;
	public $fields;
	public $primaryKeys;
	public $foreignKeys; //todo: hmm...?
	public $hasCreated;
	public $hasModified;
	public $localeId;
	public $localized;
	
	public function __construct( $db = null )
	{
		global $db; //todo: once a registry is set try to access the default db link if $db is not set.
		
		$this->className = get_class($this);
		
		$this->table = strtolower( get_class($this) . 's' );
		
		$localeTable = $db->getOne("SHOW TABLES LIKE '{$this->table}__locale';");
		if($localeTable)
		{
			$this->localized = true;
		}
		$this->tableView = $this->buildQueryTableView();
		
		$this->fields = new modelFields();

		$fields = $db->getAll("DESCRIBE {$this->table};");
		foreach($fields as $field)
		{
			$this->fields->{$field['Field']} = new modelField($field);
			//$this->fields->register( new modelField($field) ); //bug: register is not really nesessary is it?
		}

		if ($this->localized)
		{
			$fieldsLocale = $db->getAll("DESCRIBE {$this->tableLocale};");
			foreach($fieldsLocale as $fieldLocale)
			{
				$this->fields->{$fieldLocale['Field']} = new modelField($fieldLocale);
				$this->fields->{$fieldLocale['Field']}->localized = true;
				//$this->fields->register( new modelField($field) ); //bug: register is not really nesessary is it?
			}
		}
		
		$this->hasCreated = $this->fields->created ? true : false;
		$this->hasModified = $this->fields->modified ? true : false;
		
		$this->primaryKeys = new stdClass();
		
		foreach($this->fields as $field)
		{
			if($field->primaryKey)
			{
				$this->primaryKeys->{$field->name} = $field;
			}
		}
		
		krumo($this);
	}
	
	public function __call($method, $arguments)
	{
		//todo: needed functionality for: findOneByField, findAllByField
		//bug: check if field exists, clean arguments
		if(strstr($method, 'findAllBy'))
		{
			$field = str_replace('findAllBy', '', $method);
			return $this->findAllBy($field, $arguments[0]);
		}
		else if(strstr($method, 'findOneBy'))
		{
			$field = str_replace('findOneBy', '', $method);
			return $this->findOneBy($field, $arguments[0]);
		}
	}
	
	public function orderBy($key, $direction='ASC')
	{
		echo '------------------------------';
		krumo($key);
		echo '------------------------------';
		return $key;
	}
	
	public function setLocale($localeId)
	{
		if(is_int($localeId)&&($this->localized))
		{
			//bug: check if locale exists in db or something?
			$this->localeId = $localeId;
			$tableView = $this->buildQueryTableView($this->localeId);
			$this->setTableView($tableView);
		}
	}
	
	private function setTableView($query)
	{
		$this->tableView = $query;
		return $this->tableView;
	}
	
	private function buildQueryTableView($localeId=null)
	{
		if ($this->localized)
		{
			$this->tableLocale = strtolower( $this->table . '__locale' ); //todo: add this to a configuration array or sth.
			$table_id = strtolower($this->className) . '_id';
			if($this->localeId)
			{
				$tableView = " ( SELECT t.*, l.* FROM {$this->table} t JOIN {$this->tableLocale} l ON t.id = l.{$table_id} AND l.locale_id = '{$this->localeId}') AS {$this->table}";			
			}
			else
			{
				$tableView = " ( SELECT t.*, l.* FROM {$this->table} t JOIN {$this->tableLocale} l ON t.id = l.{$table_id} ) AS {$this->table}";	
			}
		}
		else
		{
			$tableView = $this->table;
		};
		
		return $tableView;	
	}
	
	public function findAll()
	{
		global $db;
		
		$query = "SELECT * FROM {$this->tableView}";
		$results = $db->getAll($query);
		$results = array( $this->className => $results);

		return $results;
	}
	
	public function findOne($id)
	{
		//bug: check if id is int and use the primary key instead of the id - primary keys can be multiple.
		global $db;
		
		$query = "SELECT * FROM {$this->tableView} WHERE id='{$id}'";
		$results = array( $this->className => $db->getRow($query) );
		
		return $results;
	}
	
	public function findOneWhere($where)
	{
		//bug: clean where?
		global $db;
		$query = "SELECT * FROM {$this->tableView} WHERE {$where}";
		return array( $this->className => $db->getRow($query) );
	}
	
	public function findAllWhere($where)
	{
		//bug: clean where?
		global $db;
		$query = "SELECT * FROM {$this->tableView} WHERE {$where}";
		return array( $this->className => $db->getAll($query) );
	}
	
	public function findAllBy($field, $value)
	{
		//todo: clean the value and the check the field if it exists...
		global $db;
		$query = "SELECT * FROM {$this->tableView} WHERE {$field} = '{$value}'";
		return array( $this->className => $db->getAll($query) );
	}
	
	public function findOneBy($field, $value)
	{
		//todo: clean the value and the check the field if it exists...
		global $db;
		$query = "SELECT * FROM {$this->tableView} WHERE {$field} = '{$value}'";
		return array( $this->className => $db->getRow($query) );
	}
	
}

?>